/*
剑指 Offer II 013. 二维子矩阵的和
给定一个二维矩阵 matrix，以下类型的多个请求：

计算其子矩形范围内元素的总和，该子矩阵的左上角为 (row1, col1) ，右下角为 (row2, col2) 。
实现 NumMatrix 类：

NumMatrix(int[][] matrix) 给定整数矩阵 matrix 进行初始化
int sumRegion(int row1, int col1, int row2, int col2) 返回左上角 (row1, col1) 、右下角 (row2, col2) 的子矩阵的元素总和。
 

示例 1：



输入: 
["NumMatrix","sumRegion","sumRegion","sumRegion"]
[[[[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]],[2,1,4,3],[1,1,2,2],[1,2,2,4]]
输出: 
[null, 8, 11, 12]

解释:
NumMatrix numMatrix = new NumMatrix([[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]]);
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (红色矩形框的元素总和)
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (绿色矩形框的元素总和)
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (蓝色矩形框的元素总和)
 

提示：

m == matrix.length
n == matrix[i].length
1 <= m, n <= 200
-105 <= matrix[i][j] <= 105
0 <= row1 <= row2 < m
0 <= col1 <= col2 < n
最多调用 104 次 sumRegion 方法
 

注意：本题与主站 304 题相同： https://leetcode-cn.com/problems/range-sum-query-2d-immutable/
*/

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix* obj = new NumMatrix(matrix);
 * int param_1 = obj->sumRegion(row1,col1,row2,col2);
 */

 #include<vector>
 #include<unordered_map>
 using namespace std;
 class NumMatrix {
public:
    //传统的解法即使用了循环2x2展开和缓存也无法满足条件,提交超时
    NumMatrix(vector<vector<int>>& matrix):mtx(matrix) {
    }
    
    //as row,col <200,so can represent in int
    int sumRegion(int row1, int col1, int row2, int col2) {
        int hashKey = (0x0 | row1) | (row2 << 8) | (col1 << 16) | (col2 << 24);

        if(results.find(hashKey) != results.end())
        {
            // std::cout<<"return direct"<<std::endl;
            return results[hashKey];
        }

        long sum = 0;
        for(int i = row1; i <= row2;++i)
        {
            long sum0 = 0;
            long sum1 = 0;
            //循环展开 2 x 2
            int j = col1;
            for(; j <= col2 - 1;j += 2)
            {
                sum0 += mtx[i][j];
                sum1 += mtx[i][j+1];
            }
            if(j <= col2)
            {
                sum0 += mtx[i][j];
            }

            sum += sum0 + sum1;
        }

        results[hashKey] = sum;
        return sum;
    }
private:
    vector<vector<int>> mtx;
    unordered_map<int,long> results;
};

//prefix sum array solution
class NumMatrix {
public:
    NumMatrix(vector<vector<int>>& matrix):sumsCol(matrix.size(),vector<int>(matrix[0].size() + 1)) {
        for(int i = 0; i < matrix.size();++i)
        {
            int sum = 0;
            //caculate prefix sum for every row
            for(int j = 0; j < matrix[0].size(); ++j)
            {
                sumsCol[i][j+1] = sumsCol[i][j] + matrix[i][j];
            }
        }
    }
    
    int sumRegion(int row1, int col1, int row2, int col2) {
        int sum = 0;
        for(int i = row1; i <= row2;++i)
        {
            sum += sumsCol[i][col2 + 1] - sumsCol[i][col1];
        }
        return sum;
    }

private:
    vector<vector<int>> sumsCol;//prefix col sum array for every row
};

//caculate 2d prefix sum
//sums[i][j] means sum of matrix[0][0] to matrix[i-1][j-1]
class NumMatrix {
public:
    NumMatrix(vector<vector<int>>& matrix):sums(matrix.size() + 1,vector<int>(matrix[0].size() + 1)) {
        for(int i = 0; i < matrix.size();++i)
        {
            int sum = 0;
            for(int j = 0; j < matrix[0].size(); ++j)
            {
                //current sum = left sum + current matrix value + this column up value
                sums[i+1][j+1] = sums[i+1][j] + matrix[i][j] + (sums[i][j+1] - sums[i][j]);
                // printf("%d ",sums[i+1][j+1]);
            }
            // printf("\n");
        }
    }
    
    int sumRegion(int row1, int col1, int row2, int col2) {
        // printf("%d %d %d %d\n",row1,col1,row2,col2);
        // printf("%d %d %d %d\n",sums[row2+1][col2+1],sums[row1][col2],sums[row2][col1],sums[row1][col1]);
        //right bottom sum subtract left and up,plus cross sums[row1][col1],which has been subtract 2 time
        //sums[row1][col2+1] left side
        //sums[row2+1][col1] up side
        return sums[row2+1][col2+1] - sums[row1][col2+1] -sums[row2+1][col1] + sums[row1][col1];
    }

private:
    vector<vector<int>> sums;//2-d prefix sum,caculate the sum from matrix[0][0] to the right bottom i,j
};